package mulis;

import dvsdk.GlobalConfig;
import dvsdk.client.CollectModel;
import dvsdk.util.Util;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author Ivan
 */
public class PrintDialog extends javax.swing.JDialog {

    private boolean approved = false;
    private boolean all = true;
    private String name = "";
    private String fields = "";

    /** Creates new form PrintDialog */
    public PrintDialog(java.awt.Frame parent, boolean modal, boolean all, String name, String fields) {
        super(parent, modal);
        initComponents();
        this.name = name;
        this.fields = fields+",";
        //this.setLocationRelativeTo(null);
        //shablonCombo.setEnabled(false);
        
        //jPanel2.setVisible(false);
        hideFields();
        fillTableFields();
        jButton1.requestFocus();
        this.all = all;
        if (all) {
            allRadio.setSelected(true);
            selectedRadio.setEnabled(false);
        } else {
            selectedRadio.setSelected(true);
        }
        
        shablonCombo.setSelectedIndex(0);
        shablonComboItemStateChanged(null);
    }

    private void fillTableFields() {
        // TODO: REFACTORING
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setNumRows(0);

        for (Iterator<CollectModel> it = GlobalConfig.LIST_MODEL.iterator(); it.hasNext();) {
            CollectModel collectModel = it.next();
            if (fields.contains(collectModel.getName()+",")) {
                Vector vv = new Vector();
                vv.add(collectModel.getName());
                vv.add(collectModel.getAlias());
                vv.add(!Util.checkExistPFieldsKey(name, collectModel.getName()));
                model.addRow(vv);
            }
        }
    }
    
    private void savePrintTableFields() {
        // TODO: REFACTORING
        DefaultTableModel mdl = (DefaultTableModel) jTable1.getModel();
        Vector data = mdl.getDataVector();
        for (int i = 0; i < data.size(); i++) {
            for (Iterator<CollectModel> it = GlobalConfig.LIST_MODEL.iterator(); it.hasNext();) {
                CollectModel collectModel = it.next();
                if (((Vector)data.get(i)).get(0).toString().equals(collectModel.getName())) {
                    String tmp0=((Vector)data.get(i)).get(2).toString();
                    if (tmp0.equals("true")) {
                        Util.removePFieldsKey(name, ((Vector)data.get(i)).get(0).toString());
                    } else {
                        Util.addPFieldsKey(name, ((Vector)data.get(i)).get(0).toString());
                    }
                }
            }
        }
    }

    private void showFields() {
        jScrollPane1.setVisible(true);
        this.setSize(283, 502);
    }

    private void hideFields() {
        jScrollPane1.setVisible(false);
        this.setSize(283, 294);
    }

    @Action
    public void escape() {
        approved = false;
        dispose();
    }
    
    @Action
    public void start_print() {
        savePrintTableFields();
        approved = true;
        dispose();
    }
    
    public boolean execute() {
        setVisible(true);
        return approved;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        shablonCombo = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        allRadio = new javax.swing.JRadioButton();
        selectedRadio = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        mapCheck = new javax.swing.JCheckBox();
        addonCheck = new javax.swing.JCheckBox();
        photoCheck = new javax.swing.JCheckBox();
        jToggleButton1 = new javax.swing.JToggleButton();
        contactCheck = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getResourceMap(PrintDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setLocationByPlatform(true);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        shablonCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Таблица", "Расширенный" }));
        shablonCombo.setName("shablonCombo"); // NOI18N
        shablonCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                shablonComboItemStateChanged(evt);
            }
        });
        shablonCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shablonComboActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(allRadio);
        allRadio.setText(resourceMap.getString("allRadio.text")); // NOI18N
        allRadio.setName("allRadio"); // NOI18N

        buttonGroup1.add(selectedRadio);
        selectedRadio.setText(resourceMap.getString("selectedRadio.text")); // NOI18N
        selectedRadio.setName("selectedRadio"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(allRadio)
                    .addComponent(selectedRadio))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(allRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectedRadio))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        mapCheck.setSelected(true);
        mapCheck.setText(resourceMap.getString("mapCheck.text")); // NOI18N
        mapCheck.setToolTipText(resourceMap.getString("mapCheck.toolTipText")); // NOI18N
        mapCheck.setName("mapCheck"); // NOI18N

        addonCheck.setSelected(true);
        addonCheck.setText(resourceMap.getString("addonCheck.text")); // NOI18N
        addonCheck.setToolTipText(resourceMap.getString("addonCheck.toolTipText")); // NOI18N
        addonCheck.setName("addonCheck"); // NOI18N
        addonCheck.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                addonCheckItemStateChanged(evt);
            }
        });

        photoCheck.setSelected(true);
        photoCheck.setText(resourceMap.getString("photoCheck.text")); // NOI18N
        photoCheck.setName("photoCheck"); // NOI18N

        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jToggleButton1ItemStateChanged(evt);
            }
        });

        contactCheck.setSelected(true);
        contactCheck.setText(resourceMap.getString("contactCheck.text")); // NOI18N
        contactCheck.setName("contactCheck"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(mapCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(photoCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(contactCheck))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(addonCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton1)))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mapCheck)
                    .addComponent(photoCheck)
                    .addComponent(contactCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addonCheck)
                    .addComponent(jToggleButton1))
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getActionMap(PrintDialog.class, this);
        jButton1.setAction(actionMap.get("start_print")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setAction(actionMap.get("escape")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Имя", "Название", "Видимость"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem2.setAction(actionMap.get("start_print")); // NOI18N
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenu1.add(jMenuItem2);

        jMenuItem1.setAction(actionMap.get("escape")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(shablonCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(shablonCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void shablonComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shablonComboActionPerformed

    }//GEN-LAST:event_shablonComboActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        savePrintTableFields();
        approved = true;
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jToggleButton1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jToggleButton1ItemStateChanged
        if (jToggleButton1.isSelected()) {
            showFields();
        } else {
            hideFields();
        }
    }//GEN-LAST:event_jToggleButton1ItemStateChanged

    private void addonCheckItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_addonCheckItemStateChanged
        if (addonCheck.isSelected()) {
            jToggleButton1.setEnabled(true);
        } else {
            jToggleButton1.setEnabled(false);
            jToggleButton1.setSelected(false);
        }
    }//GEN-LAST:event_addonCheckItemStateChanged

    private void shablonComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_shablonComboItemStateChanged
        mapCheck.setEnabled(shablonCombo.getSelectedIndex()==1);
        photoCheck.setEnabled(shablonCombo.getSelectedIndex()==1);
        addonCheck.setEnabled(shablonCombo.getSelectedIndex()==1);
    }//GEN-LAST:event_shablonComboItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JCheckBox addonCheck;
    public javax.swing.JRadioButton allRadio;
    private javax.swing.ButtonGroup buttonGroup1;
    public javax.swing.JCheckBox contactCheck;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToggleButton jToggleButton1;
    public javax.swing.JCheckBox mapCheck;
    public javax.swing.JCheckBox photoCheck;
    public javax.swing.JRadioButton selectedRadio;
    public javax.swing.JComboBox shablonCombo;
    // End of variables declaration//GEN-END:variables
}
